package com.example.myfirstapplication.adapter

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.example.myfirstapplication.R
import com.example.myfirstapplication.data.News

class NewListAdapter (private val context: Context, private val list: ArrayList<News>) : BaseAdapter() {
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var holder: NewsListViewHolder
        var v: View
        if (convertView == null) {
            v = View.inflate(context, R.layout.newslistitem_layout, null)
            holder =
                NewsListViewHolder(
                    v
                )
            v.tag = holder
        } else {
            v = convertView
            holder = v.tag as NewsListViewHolder
        }
        holder.newsTitle.text = list[position].title
        Glide.with(context).load(list[position].thumb).into(holder.newsThumb)
        return v
    }

    override fun getItem(position: Int): Any {
        return list[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getCount(): Int {
        return list.size
    }

    class NewsListViewHolder(var viewItem: View) {
        var newsTitle: TextView = viewItem.findViewById(R.id.news_Title)
        var newsThumb: ImageView = viewItem.findViewById(R.id.news_Thumb)
    }
}

